<template>
  <input
    @input="handlerInput"
    @blur="handlerBlur"
    class="myinput"
    :[pn]="{ success: flag, error: !flag }"
  />
</template>

<script>
export default {
  props: {
    rules: {
      type: RegExp,
    },
    msg: {
      type: String,
      defaule: "获取错误",
    },
  },
  data() {
    return {
      flag: "",
      pn: "aa",
    };
  },
  methods: {
    handlerInput(e) {
      let value = e.target.value;
      if (this.rules) {
        if (this.rules.test(value)) {
          this.flag = true;
        } else {
          this.flag = false;
        }
      }
      this.$emit("input", value);
    },
    handlerBlur(e) {
      let value = e.target.value;
      if (this.rules) {
        this.pn = "class";
        if (!this.rules.test(value)) {
          this.$toast.fail(this.msg);
        }
      }
    },
  },
};
</script>

<style lang='less' scoped>
.myinput {
  width: 314/360 * 100vw;
  height: 60px;
  outline: none;
  border: none;
  border-bottom: 2px solid #ccc;
  font-size: 20px;
  line-height: 60px;
}
.success {
  border-bottom-color: rgba(22, 189, 22, 0.842);
}
.error {
  border-bottom-color: rgba(228, 25, 25, 0.829);
}
</style>